<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

namespace app\license\model;

use think\facade\Db;
use think\Model;

class License extends Model
{

    public static $Status = [
        0 => '待上报',
        1 => '已上报',
        2 => '审核中',
        3 => '已公示',
        4 => '',
        5 => '',
    ];
    public static $Type = [
        0 => '自有',
        1 => '公海数据',
    ];

    //列表检索
    public function get_list($param = [], $where = [], $whereOr = [])
    {

        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $db = self::where($where)
            ->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->field('a.*')
            ->alias('a');
        if (!empty($param['cert_project_name'])) {
            $db->join('license_cert b', 'a.id = b.license_id');
            $db->where('b.delete_time', 0);
        }
        if (!empty($param['cert_cates']) || !empty($param['cert_from_type'])) {
            $db->join('license_cert b', 'a.id = b.license_id');
            $db->where('b.delete_time', 0);
            $db->join('license_cert_cates c', 'b.id = c.cert_id');
        }

        if (!empty($param['safety_valid_from'])) {
            if ($param['safety_valid_from'] == 1) {
                $db->where('a.safety_valid_date < DATE_ADD(CURDATE(), INTERVAL 6 MONTH)')->where('safety_valid_date', '<>', '0000-00-00');
            } else {
                $db->where('a.safety_valid_date > DATE_ADD(CURDATE(), INTERVAL 6 MONTH)')->where('safety_valid_date', '<>', '0000-00-00');
            }
        }
        $list = $db->group('a.id')->order('a.create_time desc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                $item->status_name = self::$Status[(int)$item->check_status];
                $cert_ids = Db::name('license_cert')->where('license_id', $item->id)->where('delete_time', 0)->column('id');
                $certs = Db::name('license_cert_cates')->field('cert_serial,cert_from,cert_level,cert_from_type')->group('cert_serial,cert_from,cert_level')->whereIn('cert_id', $cert_ids)->select()->toArray();
                $str = '';
                $cert_types = [];
                foreach ($certs as $k => $v) {
                    if ($str == '') {
                        $str .= $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    } else {
                        $str .= '|' . $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    }
                    $cert_types[] = $v['cert_from_type'];
                }
                $cert_types = implode('|', array_unique($cert_types));
                $item->projects = $str;
                $item->cert_types = $cert_types;
                $item->project_user_name = '';
                if ($item->project_user_id > 0) {
                    $item->project_user_name = Db::name('admin')->where('id', $item->project_user_id)->value('name');
                }
                if ($item->safety_valid_date == '0000-00-00') {
                    $item->safety_valid_date = '';
                }
                $item->safety_valid_type = '';
                if ($item->safety_valid_date) {
                    $times = strtotime($item->safety_valid_date) - time();
                    if ($times < 180 * 24 * 60 * 60) {
                        $item->safety_valid_type = '即将到期';
                    }
                }
            });
        return $list;
    }

    //列表检索
    public function sell_get_list($param = [], $where = [], $whereOr = [])
    {

        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $db = self::where($where)
            ->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->field('a.*')
            ->alias('a');
        if (!empty($param['cert_project_name'])) {
            $db->join('license_cert b', 'a.id = b.license_id');
            $db->where('b.delete_time', 0);
        }
        if (!empty($param['cert_cates']) || !empty($param['cert_from_type'])) {
            $db->join('license_cert b', 'a.id = b.license_id');
            $db->where('b.delete_time', 0);
            $db->join('license_cert_cates c', 'b.id = c.cert_id');
        }

        if (!empty($param['safety_valid_from'])) {
            if ($param['safety_valid_from'] == 1) {
                $db->where('a.safety_valid_date < DATE_ADD(CURDATE(), INTERVAL 6 MONTH)')->where('safety_valid_date', '<>', '0000-00-00');
            } else {
                $db->where('a.safety_valid_date > DATE_ADD(CURDATE(), INTERVAL 6 MONTH)')->where('safety_valid_date', '<>', '0000-00-00');
            }
        }
        $list = $db->limit(3)->group('a.id')->order('a.create_time desc')
            ->select()->toArray();
        foreach ($list as $k => $v) {

            $list[$k]['status_name'] = self::$Status[(int)$v['check_status']];
            $cert_ids = Db::name('license_cert')->where('license_id', $v['id'])->where('delete_time', 0)->column('id');
            $certs = Db::name('license_cert_cates')->field('cert_serial,cert_from,cert_level,cert_from_type')->whereIn('cert_id', $cert_ids)->select()->toArray();
            $str = '';
            $cert_types = [];
            foreach ($certs as $k1 => $v1) {
                if ($str == '') {
                    $str .= $v1['cert_serial'] . '-' . $v1['cert_from'] . '-' . $v1['cert_level'];
                } else {
                    $str .= '|' . $v1['cert_serial'] . '-' . $v1['cert_from'] . '-' . $v1['cert_level'];
                }
                $cert_types[] = $v1['cert_from_type'];
            }
            $cert_types = implode('|', array_unique($cert_types));
            $list[$k]['projects'] = $str;
            $list[$k]['cert_types'] = $cert_types;
            $list[$k]['project_user_name'] = '';
            if ($v['project_user_id'] > 0) {
                $list[$k]['project_user_name'] = Db::name('admin')->where('id', $v['project_user_id'])->value('name');
            }
            if ($v['safety_valid_date'] == '0000-00-00') {
                $list[$k]['safety_valid_date'] = '';
            }
            $list[$k]['safety_valid_type'] = '';
            if ($v['safety_valid_date']) {
                $times = strtotime($v['safety_valid_date']) - time();
                if ($times < 180 * 24 * 60 * 60) {
                    $v['safety_valid_type'] = '即将到期';
                }
            }
        }
        $data['data'] = $list;
        return $data;
    }


    // 获取合同详情
    public function detail($id)
    {
        $detail = self::where(['id' => $id])->find();
        if (!empty($detail)) {
            $share_names = Db::name('Admin')->where([['id', 'in', $detail['share_ids']]])->column('name');
            $detail['share_names'] = implode(',', $share_names);
            if ($detail['file_ids'] != '') {
                $fileArray = Db::name('File')->where('id', 'in', $detail['file_ids'])->select();
                $detail['fileArray'] = $fileArray;
//                $detail['license_cert'] =  Db::name('license_cert')->where('license_id', $detail['id'])->where('is_delete', 0)->select();
            }
        }
        return $detail;
    }
}
